<?php

/**
 * @filename Cate.class.php 
 * @encoding UTF-8 
 * @author yuzhiyuan <307090870@qq.com>
 * @copyright copyright 2013 (c) Emagsoftware Technology Co., Ltd. All Rights reserved.
 * @license ……
 * @datetime 2013-6-22  19:02:34
 * @version 1.0
 * @Description
  */

class Cate {
    //递归成一位数组
    public static function unlimitedForLevel($cate, $html = '—', $p_id = 0, $level = 0){
        $arr = array();
        foreach($cate as $k => $v){
            if($v['p_id'] == $p_id){
                $v['level'] = $level + 1;               
                $v['html'] = '|'. str_repeat($html, $level);                
                $arr[] = $v;
                $arr = array_merge($arr, self::unlimitedForLevel($cate, $html, $v['id'], $level + 1));
            }
        }
        return $arr;
    }
    //递归成多维数组
    public static function unlimitedForLayer($cate, $p_id = 0){
        $arr = array();
        foreach($cate as $v){
            if($v['p_id'] == $p_id){
                $v['child'] = self::unlimitedForLayer($cate, $v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
        
    }
    //查找家族树
    public static function getParents($cate, $id){
        $arr = array();
        foreach($cate as $v){
            if($v['id'] == $id){
                $arr[] = $v;
                $arr = array_merge($arr, self::getParents($cate, $v['p_id']));
            }
        }
        return $arr;
    }
    
    //查找子孙树ID
    public static function getChildId($cate, $p_id){
        $arr = array();
        foreach($cate as $v){
            if($v['p_id'] == $p_id){
                $arr[] = $v['id'];
                $arr = array_merge($arr, self::getChildId($cate, $v['id']));
            }
        }
        return $arr;        
    }
    
    //查找子孙树
    public static function getChild($cate, $p_id){
        $arr = array();
        foreach($cate as $v){
            if($v['p_id'] == $p_id){
                $arr[] = $v;
                $arr = array_merge($arr, self::getChildId($cate, $v['id']));
            }
        }
        return $arr;        
    }
}